package com.xiaoge.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.Product;
import com.xiaoge.biz.entity.Supplier;
import com.xiaoge.biz.service.SupplierService;
import com.xiaoge.biz.vo.ProductVo;
import com.xiaoge.biz.vo.SupplierVo;
import com.xiaoge.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@CrossOrigin
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询物资来源
     *
     * @param current
     * @param size
     * @param supplierVo
     * @return
     */
    @ApiOperation(value = "分页查询物资来源")
    @PostMapping("/findSupplierList")
    public Result findSupplierList(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "6") Integer size,
                                   @RequestBody SupplierVo supplierVo) {
        //取得分页条件
        Page<Supplier> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Supplier> queryWrapper = getWrapper(supplierVo);

        //执行查询
        IPage<Supplier> productPage = supplierService.page(page, queryWrapper);

        long total = productPage.getTotal();
        List<Supplier> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 获取所有来源
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取所有来源")
    @GetMapping("/getAllSupplierList")
    public Result getAllSupplierList() {
        List<Supplier> suppliers=supplierService.list();
        return Result.ok().data("records",suppliers);
    }

    /**
     * 添加来源
     *
     * @param supplier
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加来源失败", operation = "添加来源")
    @ApiOperation(value = "添加来源")
    @PostMapping("/addSupplier")
    public Result addSupplier(@RequestBody Supplier supplier) {
        supplierService.save(supplier);
        return Result.ok();
    }

    /**
     * 修改来源
     *
     * @param supplier
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改来源失败", operation = "修改来源")
    @ApiOperation(value = "修改来源")
    @PostMapping("/updateSupplier")
    public Result updateSupplier(@RequestBody Supplier supplier) {
        supplierService.updateById(supplier);
        return Result.ok();
    }

    /**
     * 删除来源
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除来源失败", operation = "删除来源")
    @ApiOperation(value = "删除来源")
    @DeleteMapping("/deleteSupplierById")
    public Result deleteSupplierById(String id) {
        supplierService.removeById(id);
        return Result.ok();
    }

    private QueryWrapper<Supplier> getWrapper(SupplierVo supplierVo) {
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        if (supplierVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(supplierVo.getName())) {
                queryWrapper.like("name", supplierVo.getName());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getAddress())) {
                queryWrapper.like("address", supplierVo.getAddress());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getContact())) {
                queryWrapper.like("contact", supplierVo.getContact());
            }
        }
        return queryWrapper;
    }
}
